package com.appspot.smartgan.server;

/**
 * The class contains methods that are used to send Http
 * requests to the app-engine server. </p>
 * Parameters are separated with &
 * URL format:</p>
 * http://smart-gan.appspot.com/action_code?parem1=..&param2=..
 * @author Dor Mesica
 *
 */
public abstract class UrlGenerator {

	/* server address on google app-engine */
	private static final String SERVER_URL = 
			"http://smart-kindergarten.appspot.com/android/";

	/* indicates the next parameter id the is used for both child and parent */
	private static final String CHILD_ID = "child_id=";

	/* indicates the next parameter is a date */
	private static final String DATE = "date=";

	private static final String NONE = ""; 

	private static final String KG_ID = "KG_id=";

	/* indicates the next parameter is the id of the parent 
	 * when both parent and child id are required 
	 */
	private static final String PARENT_ID = "parent_email_address=";

	private static final String REGISTRATION_ID = "reg_id=";

	private static final String TITLE = "title=";

	private static final String MESSAGE = "msg=";

	private static final String TO_TEACHER = "to_teacher=";



	/**
	 * Sends a request for the server to retrieve all children
	 * associated with the given parent.
	 * @param parentID The id of the parent for which to retrieve the list of children
	 * @return List of children for the given parent.
	 */
	public static String getChildren(String parentID) {
		String url = SERVER_URL;
		url += "getChildren?";		// set the purpose of the request

		// add parameters to request 
		url = addParameter(url, NONE, parentID, true);

		return url;
	}

	/**
	 * Sends a request to the server asking for the child's
	 * attendance.
	 * @param id Child's ID
	 * @return true iff the child as arrived at school
	 */
	public static String getChildAttendance(String id, String date) {
		String url = SERVER_URL;
		url += "getChildAttendance?";		// set the purpose of the request

		// add parameters to request
		url = addParameter(url, CHILD_ID, id, false);
		url = addParameter(url, DATE, date, true);

		return url;
	}

	/**
	 * Sends a request to the server asking for all
	 * the information about the child with the given
	 * ID. 
	 * @param childId The child for which the information is required.
	 * @return An array of strings containing the information as follows: </p>
	 * 			$return[0] = name, </p>
	 * 			$return[1] = address, </p>
	 * 			$return[2] = phone, </p>
	 *  		$return[3] = birth date, </p>
	 * 			$return[4] = gender, </p>
	 * 			$return[5] = school, </p> 			
	 */
	public static String getChildInfo(String childId) {
		String url = SERVER_URL;
		url += "getChildInfo?";

		// adding parameters
		url = addParameter(url, NONE, childId, true);

		return url;
	}

	/**
	 * 
	 * @param regid
	 * @param parentId
	 * @return
	 */
	public static String registrationId(String regid, String parentId) {
		String url = SERVER_URL;
		url += "addRegId?";

		url = addParameter(url, PARENT_ID, parentId, false);
		url = addParameter(url, REGISTRATION_ID, regid, true);

		return url;
	}

	/**
	 * 
	 * @param childId
	 * @return
	 */
	public static String getSchoolId(String childId) {
		String url = SERVER_URL;
		url += "getKGidByChildId?";

		url = addParameter(url, CHILD_ID, childId, true);

		return url;
	}

	/**
	 * 
	 * @param schoolId
	 * @return
	 */
	public static String getSchoolInfo(String schoolId) {
		String url = SERVER_URL;
		url += "getKGInfo?";

		url = addParameter(url, KG_ID, schoolId, true);

		return url;
	}

	public static String getMessageBoard(String schoolId) {
		String url = SERVER_URL;
		url += "getAllMsgByKG?";

		url = addParameter(url, KG_ID, schoolId, true);

		return url;
	}

	public static String deleteRegId(String parentId) {
		String url = SERVER_URL;
		url += "deleteRegId?";

		url = addParameter(url, PARENT_ID, parentId, true);

		return url;
	}

	public static String getPrivateMessages(String childId, String schoolId) {
		String url = SERVER_URL;
		url += "getAllMsgByChild?";

		url = addParameter(url, CHILD_ID, childId, false);
		url = addParameter(url, KG_ID, schoolId, true);

		return url;
	}

	public static String getContactList(String schoolId) {
		String url = SERVER_URL;
		url += "getChildrenIdsByKGid?";

		url = addParameter(url, KG_ID, schoolId, true);

		return url;
	}

	public static String sendMessageToTeacher(String title, String message,
			String childId, String schoolId) {
		String url = SERVER_URL;
		url += "sendMessageToTeacher?";

		title = title.replaceAll(" ", "%20");
		message = message.replaceAll(" ", "%20");
		
		title = title.replaceAll("\n", "%40");
		message = message.replaceAll("\n", "%40");
		
		url = addParameter(url, TITLE, title, false);
		url = addParameter(url, KG_ID, schoolId, false);
		url = addParameter(url, MESSAGE, message, false);
		url = addParameter(url, TO_TEACHER, "True", false);
		url = addParameter(url, CHILD_ID, childId, true);
		
		return url;
	}


	// private methods

	/* 
	 * adds a parameter to the given url and ; if this is not the last parameter
	 * that should be added
	 */
	private static String addParameter(String url, String type, 
			String value, boolean isLast) {
		url += type + value;

		if (!isLast) {		// if not last add ;
			url += "&";
		}

		return url;
	}

}
